<template>
  <div>
    <div class="titletopXX">
      <van-nav-bar
        size="30"
        title="图片列表"
        right-text="确认"
        left-arrow
        :fixed="true"
        @click-left="onClickLeft"
        @click-right="onClickright"
      />
    </div>
    <van-search
      v-model="XXyz.name"
      placeholder="请输入搜索关键词"
      @search="searchXX"
      @clear="onRefresh"
    />
    <!-- <headerXX :name="title"></headerXX> -->
    <van-pull-refresh
      class="pull-refresh_Search"
      v-model="refreshing"
      @refresh="onRefresh"
    >
      <van-empty
        v-if="list.length == 0 && !error"
        image="search"
        description="未找到数据"
      />
      <van-empty v-if="error" image="error" description="网络错误" />
      <van-list
        :immediate-check="false"
        v-model:loading="loading"
        :finished="finished"
        :error.sync="error"
        error-text="请求失败，点击重新加载"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <!-- 列表内容 -->
        <div class="ulbox" v-if="list.length > 0">
          <div class="ul" v-for="(item, index) in list" :key="index">
            <div class="li" @click="GXXX(item)">
              <van-image width="100%" height="130px" :src="item.image" />
              <div class="WZ_size_14 title">{{ item.name }}</div>
              <div class="GX" @click.stop="GXXX2(item)">
                <van-checkbox v-model="item.checked"></van-checkbox>
              </div>
            </div>
          </div>
        </div>
        <template #error>
          <div class="error-tip" @click="retryLoad">
            <van-icon name="warning" />
            <span>加载失败，点击重试</span>
          </div>
        </template>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { queryImageLibrary } from "@/api/realAssets/icsharedstation/CPXX/index.js";
export default {
  data() {
    return {
      img: "",
      list2: [],
      title: "图片列表",
      siteData: JSON.parse(localStorage.getItem("siteData")),
      XXyz: {
        siteId: JSON.parse(localStorage.getItem("siteData")).id,
        name: "",
        pageNumber: 1,
        pageSize: 10,
      },
      list: [],
      finished: false,
      error: false,
      loading: false,
      refreshing: false,
    };
  },
  created() {},
  mounted() {
    this.onLoad();
  },
  methods: {
    // 点击重试
    retryLoad() {
      this.XXyz.pageNumber = 1;
      this.XXyz.pageSize = 10;
      this.finished = false;
      this.error = false;
      this.loading = true;
      this.refreshing = true;
      this.onLoad();
    },
    // 下拉刷新
    onRefresh() {
      //   list.value = [];
      this.XXyz.pageNumber = 1;
      this.XXyz.pageSize = 10;
      this.finished = false;
      this.error = false;
      this.loading = true;
      this.refreshing = true;
      this.onLoad();
    },
    //确认提交
    onClickright() {
      if (
        this.$route.query.XX == "修改菜品" ||
        this.$route.query.XX == "新增菜品"
      ) {
        this.$router.push({
          name: "rvicsharedstation-HT_ZGST_CPGL_SetXX",
          query: {
            XX: this.$route.query.XX, // 序列化对象
            obj: this.$route.query.obj,
            img: this.img,
          },
        });
      } else {
        //返回果切
        this.$router.push({
          name: "i_life_ZGST_GQGL_SetXX",
          query: {
            XX: this.$route.query.XX, // 序列化对象
            obj: this.$route.query.obj,
            img: this.img,
          },
        });
      }
    },
    onClickLeft() {
      if (this.$route.query.XX == "新增菜品") {
        this.img = "";
      }
      if (
        this.$route.query.XX == "修改菜品" ||
        this.$route.query.XX == "新增菜品"
      ) {
 this.$router.push({
          name: "rvicsharedstation-HT_ZGST_CPGL_SetXX",
          query: {
            XX: this.$route.query.XX, // 序列化对象
            obj: this.$route.query.obj,
            img: this.img,
          },
        });
      } else {
        //返回果切
        this.$router.push({
          name: "i_life_ZGST_GQGL_SetXX",
          query: {
            XX: this.$route.query.XX, // 序列化对象
            obj: this.$route.query.obj,
            img: this.img,
          },
        });
      }
    },
    searchXX() {
      this.list2 = [];
      this.list.forEach((item) => {
        if (item.checked) {
          this.list2.push(item);
        }
      });
      this.onRefresh();
    },
    //勾选信息
    GXXX(val) {
      this.img = "";
      this.list.forEach((item) => {
        if (item.image == val.image) {
          item.checked = true;
          this.img = item.image;
        } else {
          item.checked = false;
        }
      });
    },
    GXXX2(val) {
      this.img = "";
      this.list.forEach((item) => {
        if (item.image == val.image) {
          item.checked = true;
          this.img = item.image;
        } else {
          item.checked = false;
        }
      });
      //   console.log(list.value);
    },
    // 加载数据
    onLoad() {
      this.loading = true;
      if (this.error) return; // 如果当前是错误状态，不自动触发加载
      if (this.refreshing) {
        this.list = [];
      }
      queryImageLibrary(this.XXyz).then((res) => {
        if (res.resultMessage != "SUCCESS") {
          this.list = [];
          this.error = true; // 显示错误提示
          // loading.value = false;
        } else if (res.data.length == 0) {
          this.list = [];
          this.finished = true;
        } else {
          // 使用 for 循环添加字段
          for (let i = 0; i < res.data.length; i++) {
            res.data[i].checked = false;
          }
          //搜索后信息回显
          if (this.list2.length > 0) {
            res.data.forEach((item2) => {
              this.list2.forEach((item) => {
                if (item2.image == item.image) {
                  // 直接复制属性，而不是改变引用
                  Object.assign(item2, item);
                }
              });
            });
          }
          //数据回显
          //   console.log(JSON.parse(decodeURIComponent(route.query.obj)));
          let XXimg = this.$route.query.img;
          this.img = XXimg;
          if (this.$route.query.obj != undefined) {
            res.data.forEach((item2) => {
              if (item2.image == XXimg) {
                item2.checked = true;
                //    == 0 ?  : ,//type是推荐1，不推荐给空
              }
            });
          }

          this.list = [...this.list, ...res.data];
        }
        this.loading = false;
        // 判断是否已加载完所有数据
        if (this.list.length == res.pageNum) {
          this.finished = true;
        } else {
          this.XXyz.pageNumber++;
        }
        this.refreshing = false;
      });
    },
  },
};
</script>

<style lang="less" scoped>
@import "../../../../../../static/css/index.less";
.ulbox {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 0 10px;
}
.ul {
  width: 31%;
  margin-bottom: 10px;
  border: 1px solid #999;
  .li {
    text-align: center;
    position: relative;
    .GX {
      position: absolute;
      top: 10px;
      right: 10px;
    }
    .title {
      // margin-top: 10px;
      margin-bottom: 10px;
    }
    // width: 33%;
  }
}
</style>